package com.real.operationalcontrols.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.google.android.material.tabs.TabLayout;
import com.next.easynavigation.view.CustomViewPager;
import com.real.operationalcontrols.R;
import com.real.operationalcontrols.基类.MainBaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class FragmentTask extends MainBaseFragment {
    @BindView(R.id.tab)
    TabLayout tab;
    @BindView(R.id.vp)
    CustomViewPager vp;
    Unbinder unbinder;
    List<Fragment> fragmentList = new ArrayList<>();
    String[] tabs = {"未完成", "已完成"};
    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_task_layout;
    }

    @Override
    protected void loadData() {
        MyAdapter adapter = new MyAdapter(getChildFragmentManager());
        vp.setCanScroll(false);
        vp.setAdapter(adapter);
        vp.setOffscreenPageLimit(1);
        tab.setupWithViewPager(vp);
        tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                vp.setCurrentItem(tab.getPosition(), true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        tab.addTab(tab.newTab().setText("未完成"));
        tab.addTab(tab.newTab().setText("已完成"));
        fragmentList.add(new FragmentTaskNo());
        fragmentList.add(new FragmentTaskYes());
        return rootView;
    }

    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return fragmentList.get(i);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return tabs[position];
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
